<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-show="swith">
    <el-form-item label="是否接单">
      <el-switch :width='switchWidth' @change="submit"
                 v-model="form.isReceiveOrder"
                 active-text="开启"
                 inactive-text="关闭" active-value="1" inactive-value="0">
      </el-switch>
    </el-form-item>
    <!--    <el-form-item>-->
    <!--      <el-button type="primary" size="mini" @click="submit">保存</el-button>-->
    <!--      <el-button type="danger" size="mini" @click="close">关闭</el-button>-->
    <!--    </el-form-item>-->
  </el-form>
</template>

<script>
import {getUserProfile, updateUserProfile} from "@/api/system/user";
import {getReceiveOrderMainSwitchByUserId} from "@/api/hmk/order";

export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      switchWidth: 50,
      swith: null,
      form: {
        isReceiveOrder: null,
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    //查询按钮是否展示
    this.hjf();
  },
  watch: {},
  methods: {
    hjf() {
      getReceiveOrderMainSwitchByUserId({'userId': this.$store.state.user.id}).then(response => {
        this.swith = response.data.swith;
        if (this.swith) {
          getUserProfile().then(response => {
            if (null != response.data && undefined != response.data) {
              this.form.isReceiveOrder = response.data.isReceiveOrder;
            }
          })
        }
      })
    },

    submit() {
      //修改是否接单
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserProfile(this.form).then(response => {
            this.hjf();
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
